<?php
namespace zzcms\common\model;


/**
 * 用户组数据表名
 * Class AuthGroup
 * @package zzcms\admin\model
 */
class Article extends Base
{
    protected $pk = 'id';
    protected $name = 'article';
    protected $autoWriteTimestamp = true;
    protected $createTime = 'create_time';
    protected $updateTime = 'update_time';
    /**
     * 获取记录
     * @return array
     */
    public function getRecord($option = [])
    {
        $where = isset($option['where']) ? $option['where'] : [];
        $param = isset($option['param']) ? $option['param'] : [];
        $order = isset($option['order']) ? $option['order'] : "a.listorder desc,a.id desc";
        if (empty($param)) {
            $list = $this->alias('a')->field('a.*,b.content')
                ->join("{$this->pre}article_data b", "b.key_id=a.id")
                ->order($order)->where($where)->paginate(20);
        } else {
            $list = $this->alias('a')->field('a.*,b.content')
                ->join("{$this->pre}article_data b", "b.key_id=a.id")
                ->order($order)->where($where)->paginate(20, false, ['query' => $param]);
        }

        return $list;
    }


    /**
     * 获取详细
     * @param $id   主键ID
     * @return array
     * @throws \think\Exception
     */
    public function getDetail($id)
    {
        $info = $this->get($id);
        if (empty($info)) {
            $this->error = '信息不存在';
            return false;
        }
        $model = new ArticleData();
        $info2 = $model->get(['key_id' => $id]);
        $info['content'] = isset($info2['content']) ? $info2['content'] : '';
        return is_array($info) ? $info : $info->toArray();
    }


    /**
     * 添加
     * @param $data
     * @return bool
     */
    public function addDetail(array $data)
    {
        $id = $this->isUpdate(false)->allowField(true)->save($data);
        if ($id) {
            if (false === (new ArticleData())->addData(['key_id' => $this->id, 'content' => $data['content']])) {
                //删除已添加的数据回滚
                $this->deleteData($this->id);
                $this->error = '写入副表失败！';
                return false;
            }
            return $this->id;
        } else {
            $this->error = '新增失败！';
            return false;
        }
    }


    /**
     * 编辑
     * @param type $data 提交数据
     * 同时需要更新订时器任务数量，更新（数据更新前台的两个订时器的数量）
     * @return boolean
     */
    public function editDetail($data, $id = 0)
    {
        if (empty ($data)) {
            return false;
        }
        $id = $id ? $id : (int)$data['id'];
        if (!$id) {
            $this->error = '参数不能为空！';
            return false;
        }
        $info = $this->get($id);
        if (empty($info)) {
            $this->error = '信息不存在！';
            return false;
        }
        $data['id'] = $id;
        if (false !== $this->isUpdate(true)->allowField(true)->save($data)) {
            $model = new ArticleData();
            if (false === $model->where(['key_id' => $id])->setField(['content' => $data['content']])) {
                //回滚信息
                $info->save();
                $this->error = '编辑失败！';
                return false;
            }
            return true;
        } else {
            $this->error = '编辑失败！';
            return false;
        }

    }


    /**
     * 编辑
     * @param type $data 提交数据
     * 同时需要更新订时器任务数量，更新（数据更新前台的两个订时器的数量）
     * @return boolean
     */
    public function deleteDetail($id = 0)
    {
        if (!$id) {
            $this->error = '参数不能为空！';
            return false;
        }
        $info = $this->get($id);
        if (empty($info)) {
            $this->error = '信息不存在！';
            return false;
        }
        if (false === $this->where(['id' => $id])->setField(['status' => 2])) {
            $this->error = '删除失败！';
            return false;
        }
        return true;
    }


}